using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class ClientRemindItem
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("RemindId", "HL_ClientRemindItem");
		}
		public bool Exists(int RemindId)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HL_ClientRemindItem");
			stringBuilder.Append(" where RemindId=@RemindId");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@RemindId", SqlDbType.Int, 4)
			};
			array[0].Value = RemindId;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.ClientRemindItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HL_ClientRemindItem(");
			stringBuilder.Append("Creater,CreateTime,RemindTime,RelationType,RemindContent,CorrelativeClient,CorrelativeContacter,RemindByTips,RemindByEmail,RemindBySms,Status)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@Creater,@CreateTime,@RemindTime,@RelationType,@RemindContent,@CorrelativeClient,@CorrelativeContacter,@RemindByTips,@RemindByEmail,@RemindBySms,@Status)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Creater", SqlDbType.NVarChar, 50),
				new SqlParameter("@CreateTime", SqlDbType.DateTime),
				new SqlParameter("@RemindTime", SqlDbType.DateTime),
				new SqlParameter("@RelationType", SqlDbType.NVarChar, 30),
				new SqlParameter("@RemindContent", SqlDbType.NVarChar, 200),
				new SqlParameter("@CorrelativeClient", SqlDbType.Int, 4),
				new SqlParameter("@CorrelativeContacter", SqlDbType.NVarChar, 20),
				new SqlParameter("@RemindByTips", SqlDbType.Bit, 1),
				new SqlParameter("@RemindByEmail", SqlDbType.Bit, 1),
				new SqlParameter("@RemindBySms", SqlDbType.Bit, 1),
				new SqlParameter("@Status", SqlDbType.Bit, 1)
			};
			array[0].Value = model.Creater;
			array[1].Value = model.CreateTime;
			array[2].Value = model.RemindTime;
			array[3].Value = model.RelationType;
			array[4].Value = model.RemindContent;
			array[5].Value = model.CorrelativeClient;
			array[6].Value = model.CorrelativeContacter;
			array[7].Value = model.RemindByTips;
			array[8].Value = model.RemindByEmail;
			array[9].Value = model.RemindBySms;
			array[10].Value = model.Status;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.ClientRemindItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ClientRemindItem set ");
			stringBuilder.Append("Status=@Status");
			if (!string.IsNullOrEmpty(model.Creater))
			{
				stringBuilder.Append(",Creater='" + model.Creater + "'");
			}
			if (model.RemindTime.HasValue)
			{
				stringBuilder.Append(",RemindTime='" + model.RemindTime + "'");
			}
			if (!string.IsNullOrEmpty(model.RelationType))
			{
				stringBuilder.Append(",RelationType='" + model.RelationType + "'");
			}
			if (!string.IsNullOrEmpty(model.RemindContent))
			{
				stringBuilder.Append(",RemindContent='" + model.RemindContent + "'");
			}
			if (!string.IsNullOrEmpty(model.CorrelativeClient.ToString()))
			{
				stringBuilder.Append(",CorrelativeClient=" + model.CorrelativeClient);
			}
			if (!string.IsNullOrEmpty(model.CorrelativeContacter))
			{
				stringBuilder.Append(",CorrelativeContacter='" + model.CorrelativeContacter + "'");
			}
			stringBuilder.Append(",RemindByTips=@RemindByTips");
			stringBuilder.Append(",RemindByEmail=@RemindByEmail");
			stringBuilder.Append(",RemindBySms=@RemindBySms");
			stringBuilder.Append(" where RemindId=@RemindId");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Status", SqlDbType.Bit, 1),
				new SqlParameter("@RemindByTips", SqlDbType.Bit, 1),
				new SqlParameter("@RemindByEmail", SqlDbType.Bit, 1),
				new SqlParameter("@RemindBySms", SqlDbType.Bit, 1),
				new SqlParameter("@RemindId", SqlDbType.Int, 4)
			};
			array[0].Value = model.Status;
			array[1].Value = model.RemindByTips;
			array[2].Value = model.RemindByEmail;
			array[3].Value = model.RemindBySms;
			array[4].Value = model.RemindId;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int RemindId)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientRemindItem ");
			stringBuilder.Append(" where RemindId=@RemindId");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@RemindId", SqlDbType.Int, 4)
			};
			array[0].Value = RemindId;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string RemindIdlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HL_ClientRemindItem ");
			stringBuilder.Append(" where RemindId in (" + RemindIdlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.ClientRemindItem GetModel(int RemindId)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 RemindId,Creater,CreateTime,RemindTime,RelationType,RemindContent,CorrelativeClient,CorrelativeContacter,RemindByTips,RemindByEmail,RemindBySms,Status from HL_ClientRemindItem ");
			stringBuilder.Append(" where RemindId=@RemindId");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@RemindId", SqlDbType.Int, 4)
			};
			array[0].Value = RemindId;
			HKB.Data.Model.ClientRemindItem clientRemindItem = new HKB.Data.Model.ClientRemindItem();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.ClientRemindItem result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.ClientRemindItem DataRowToModel(DataRow row)
		{
			HKB.Data.Model.ClientRemindItem clientRemindItem = new HKB.Data.Model.ClientRemindItem();
			if (row != null)
			{
				if (row["RemindId"] != null && row["RemindId"].ToString() != "")
				{
					clientRemindItem.RemindId = int.Parse(row["RemindId"].ToString());
				}
				if (row["Creater"] != null)
				{
					clientRemindItem.Creater = row["Creater"].ToString();
				}
				if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
				{
					clientRemindItem.CreateTime = new System.DateTime?(System.DateTime.Parse(row["CreateTime"].ToString()));
				}
				if (row["RemindTime"] != null && row["RemindTime"].ToString() != "")
				{
					clientRemindItem.RemindTime = new System.DateTime?(System.DateTime.Parse(row["RemindTime"].ToString()));
				}
				if (row["RelationType"] != null)
				{
					clientRemindItem.RelationType = row["RelationType"].ToString();
				}
				if (row["RemindContent"] != null)
				{
					clientRemindItem.RemindContent = row["RemindContent"].ToString();
				}
				if (row["CorrelativeClient"] != null && row["CorrelativeClient"].ToString() != "")
				{
					clientRemindItem.CorrelativeClient = new int?(int.Parse(row["CorrelativeClient"].ToString()));
				}
				if (row["CorrelativeContacter"] != null)
				{
					clientRemindItem.CorrelativeContacter = row["CorrelativeContacter"].ToString();
				}
				if (row["RemindByTips"] != null && row["RemindByTips"].ToString() != "")
				{
					if (row["RemindByTips"].ToString() == "1" || row["RemindByTips"].ToString().ToLower() == "true")
					{
						clientRemindItem.RemindByTips = true;
					}
					else
					{
						clientRemindItem.RemindByTips = false;
					}
				}
				if (row["RemindByEmail"] != null && row["RemindByEmail"].ToString() != "")
				{
					if (row["RemindByEmail"].ToString() == "1" || row["RemindByEmail"].ToString().ToLower() == "true")
					{
						clientRemindItem.RemindByEmail = true;
					}
					else
					{
						clientRemindItem.RemindByEmail = false;
					}
				}
				if (row["RemindBySms"] != null && row["RemindBySms"].ToString() != "")
				{
					if (row["RemindBySms"].ToString() == "1" || row["RemindBySms"].ToString().ToLower() == "true")
					{
						clientRemindItem.RemindBySms = true;
					}
					else
					{
						clientRemindItem.RemindBySms = false;
					}
				}
				if (row["Status"] != null && row["Status"].ToString() != "")
				{
					if (row["Status"].ToString() == "1" || row["Status"].ToString().ToLower() == "true")
					{
						clientRemindItem.Status = true;
					}
					else
					{
						clientRemindItem.Status = false;
					}
				}
			}
			return clientRemindItem;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select RemindId,Creater,CreateTime,RemindTime,RelationType,RemindContent,CorrelativeClient,CorrelativeContacter,RemindByTips,RemindByEmail,RemindBySms,Status ");
			stringBuilder.Append(" FROM HL_ClientRemindItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" RemindId,Creater,CreateTime,RemindTime,RelationType,RemindContent,CorrelativeClient,CorrelativeContacter,RemindByTips,RemindByEmail,RemindBySms,Status ");
			stringBuilder.Append(" FROM HL_ClientRemindItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HL_ClientRemindItem ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.RemindId desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HL_ClientRemindItem T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public HKB.Data.Model.ClientRemindItem GetRemindItem(string whereStr)
		{
			string text = "select * from HL_ClientRemindItem where " + whereStr;
			HKB.Data.Model.ClientRemindItem result = null;
			DataSet dataSet = Conn.Query(text.ToString(), new IDbDataParameter[0]);
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			return result;
		}
		public bool UpdateStatus(HKB.Data.Model.ClientRemindItem model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HL_ClientRemindItem set ");
			stringBuilder.Append("Status=@Status");
			stringBuilder.Append(" where RemindId=@RemindId");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@Status", SqlDbType.Bit, 1),
				new SqlParameter("@RemindId", SqlDbType.Int, 4)
			};
			array[0].Value = model.Status;
			array[1].Value = model.RemindId;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
	}
}
